package com.infra.server.service;

import com.infra.server.entity.SysUserAuth;
import com.infra.server.mapper.SysUserAuthMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author win
 * @date 2021/11/12 16:22
 * 异步任务
 */
@Slf4j
@Component
public class AsyncTask {

    @Resource
    private SysUserAuthMapper sysUserAuthMapper;


    /**
     * 记录用户登录信息
     * @param ip 登录ip
     * @param isLogin 是否是登录
     * @param username 用户名
     */
    @Async
    public void updateUserAuth(String username, String ip, boolean isLogin) {
        SysUserAuth sysUserAuth = new SysUserAuth();
        sysUserAuth.setUsername(username);
        sysUserAuth.setLoginTime(isLogin ? new Date() : null);
        sysUserAuth.setLoginIp(isLogin ? ip : null);
        sysUserAuth.setOnline(isLogin);
        sysUserAuthMapper.update(sysUserAuth);
    }
}
